package com.yst.suivi.services.vacationday;

import java.util.Collection;
import java.util.Iterator;

import javax.ejb.CreateException;
import javax.ejb.EJBException;
import javax.ejb.FinderException;
import javax.ejb.RemoveException;
import javax.ejb.SessionBean;
import javax.naming.NamingException;

import com.yst.suivi.data.vacationday.VacationDay;
import com.yst.suivi.data.vacationday.VacationDayLocalHome;
import com.yst.suivi.data.vacationday.VacationDayUtil;
import com.yst.suivi.services.AbstractSessionBean;

/**
 * @ejb.bean display-name="VacationDayService" jndi-name="VacationDayHomeRemote"
 *           name="VacationDayView" type="Stateless" view-type="both"
 */
public class VacationDayServiceBean extends AbstractSessionBean implements SessionBean
{
	/**
	 * @throws CreateException
	 * @ejb.create-method view-type = "both"
	 */
	public void ejbCreate() throws CreateException
	{
		// by default nothing to do
	}

	/**
	 * This method create a new Folder.
	 * 
	 * @param standard
	 * @return standard updated
	 * @throws CreateException
	 * @ejb.interface-method view-type = "both"
	 * @ejb.transaction type = "Required"
	 */
	public String createVacationDay(int jour, int mois, int annee) throws NamingException, CreateException
	{
		checkConstraints(jour, mois, annee, "".intern());
		VacationDayLocalHome home = VacationDayUtil.getLocalHome();
		VacationDay vacationDay = home.create();
		updateObject(jour, mois, annee, vacationDay);
		return vacationDay.getId();
	}

	/**
	 * The method checks if all the business constraints are verified.
	 * 
	 * @param login
	 * @param nom
	 * @param prenom
	 */
	private void checkConstraints(int jour, int mois, int annee, String id)
	{
		StringBuffer buf = new StringBuffer();
		buf.append(" AND jour=").append(jour);
		buf.append(" AND mois=").append(mois);
		buf.append(" AND annee=").append(annee);
		checkUnicity(id, "vacationday", "id", id, buf.toString());
	}

	/**
	 * This method update a VacationDay.
	 * 
	 * @return standard updated
	 * @throws NamingException
	 * @throws FinderException
	 * @ejb.interface-method view-type = "both"
	 * @ejb.transaction type = "Required"
	 */
	public void storeVacationDay(String id, int jour, int mois, int annee) throws FinderException, NamingException
	{

		VacationDayLocalHome home = VacationDayUtil.getLocalHome();
		VacationDay vacationDay = home.findByPrimaryKey(id);
		checkConstraints(jour, mois, annee, id);
		updateObject(jour, mois, annee, vacationDay);
	}

	/**
	 * La methode remplie l'objet EJB avec les valeurs fournies
	 * 
	 * @param annee
	 * @param mois
	 * @param jour
	 * @param tpsPos
	 * @param prj
	 */
	private void updateObject(int jour, int mois, int annee, VacationDay vacationDay)
	{
		vacationDay.setJour(new Integer(jour));
		vacationDay.setMois(new Integer(mois));
		vacationDay.setAnnee(new Integer(annee));
	}

	/**
	 * This method delete a VacationDay.
	 * 
	 * @param standard
	 * @throws NamingException
	 * @throws RemoveException
	 * @throws EJBException
	 * @throws FinderException
	 * @ejb.interface-method view-type = "both"
	 * @ejb.transaction type = "Required"
	 */
	public void deleteVacationDay(String id) throws NamingException, EJBException, RemoveException
	{
		VacationDayLocalHome home = VacationDayUtil.getLocalHome();
		home.remove(id);
	}

	/**
	 * The method returns all the vacationDay of the system
	 * 
	 * @return
	 * @throws FinderException
	 * @throws NamingException.
	 * @ejb.interface-method view-type = "both"
	 */
	public Collection findVacationDays() throws FinderException, NamingException
	{
		return VacationDayUtil.getLocalHome().findAll();
	}

	/**
	 * The method returns the VacationDay identify by the primary key
	 * 
	 * @param VacationDayKey
	 * @return
	 * @throws FinderException
	 * @throws NamingException.
	 * @ejb.interface-method view-type = "both"
	 */
	public VacationDay findVacationDayByKey(String VacationDayKey) throws FinderException, NamingException
	{
		return VacationDayUtil.getLocalHome().findByPrimaryKey(VacationDayKey);
	}

	/**
	 * the method returns true only if the jour, mois, annee is marked as
	 * vacation day
	 * 
	 * @param jour
	 * @param mois
	 * @param annee
	 * @return
	 * @ejb.interface-method view-type = "both"
	 */
	public boolean isVacationDay(int jour, int mois, int annee)
	{
		boolean isVacation = false;
		VacationDay vacation;
		try
		{
			Collection someVacations = VacationDayUtil.getLocalHome().findAll();
			Iterator itDay = someVacations.iterator();
			while (itDay.hasNext())
			{
				vacation = (VacationDay) itDay.next();
				// +1 on month because storage between java and vacation day doesn't start at the same level
				if (vacation.getJour().intValue() == jour && vacation.getMois().intValue() == (mois + 1) && (vacation.getAnnee().intValue() == annee || vacation.getAnnee().intValue() == 0))
				{
					isVacation = true;
				}
			}
		}
		catch (FinderException e)
		{
			// if error occurs, we consider the day as normal working day
			isVacation = false;
		}
		catch (NamingException e)
		{
			// if error occurs, we consider the day as normal working day
			isVacation = false;
		}

		return isVacation;
	}
}
